#include "wap_messagebox.h"
#include "wa_wm.h"
#include "wa_win.h"
#include "wa_screen.h"
#include "wa_label.h"
#include "wap.h"

static void wap_messagebox_create(void);
static void wap_messagebox_del(void);
static void wap_messagebox_callback(wa_sign* sign);

static void wap_messagebox_ms_create(void);
static void wap_messagebox_ms_del(void);
static void wap_messagebox_ms_callback(wa_sign* sign);

wa_screen_obj wap_messagebox_screen = 
{
	.id = WAP_WIN_MESSAGEBOX_ID,
	.create = wap_messagebox_create,
	.del = wap_messagebox_del,
};

wa_screen_obj wap_messagebox_ms_screen = 
{
	.id = WAP_WIN_MESSAGEBOX_MS_ID,
	.create = wap_messagebox_ms_create,
	.del = wap_messagebox_ms_del,
};

wa_obj* wap_messagebox_win;
wa_obj* wap_messagebox_ms;
wa_obj* wap_messagebox_label_win;
wa_obj* wap_messagebox_label_ms;

extern wa_font font_song_16_ascii;

uint8_t wap_messagebox_ms_return;

void wap_messagebox_init(void)
{
	wap_messagebox_ms_return = 0;
}

static void wap_messagebox_create(void)
{
	if(wap_messagebox_ms_return == 0)
	{
		wap_messagebox_win = wa_win_create(NULL);
		wa_win_set_pos(wap_messagebox_win, 0, 0);
		wa_win_set_size(wap_messagebox_win, WA_LCD_W, WA_LCD_H);
		wa_win_set_edge_show(wap_messagebox_win, wa_nok);
		wa_win_set_callback(wap_messagebox_win, wap_messagebox_callback);
		wap_messagebox_screen.win = wap_messagebox_win;
		
		wap_messagebox_label_win = wa_label_create(NULL);
		wa_win_add_obj(wap_messagebox_win, wap_messagebox_label_win);
		wa_obj_set_pos(wap_messagebox_label_win, 0, 0);
		wa_obj_set_size(wap_messagebox_label_win, WA_LCD_W, WA_LCD_H);
		wa_label_set_font(wap_messagebox_label_win, &font_song_16_ascii);
		wa_label_set_text(wap_messagebox_label_win, "press enter\nto call\nmessagebox");
		wa_label_set_col_space(wap_messagebox_label_win, 0);
		wa_label_set_row_space(wap_messagebox_label_win, 0);
		wa_label_set_text_color(wap_messagebox_label_win, WA_COLOR_FOREGROUND);
		wa_label_set_back_color(wap_messagebox_label_win, WA_COLOR_BACKGROUND);
		wa_label_set_align_mode(wap_messagebox_label_win, WA_LABEL_AL_HCENTER | WA_LABEL_AL_VCENTER);
	}
	else
	{
		wap_messagebox_ms_return = 0;
	}
}

static void wap_messagebox_del(void)
{
	if(wap_messagebox_ms_return == 0)
	{
		wa_obj_del(wap_messagebox_win);
	}
}

static void _wap_messagebox_paint(wa_obj* obj)
{
}

static void wap_messagebox_callback(wa_sign* sign)
{
	int sign_id;
	wa_obj* obj;
	sign_id = sign->sign_id;
	obj = sign->obj_dst;

	switch (sign_id)
	{
		case WA_SIGNAL_PAINT:
		{
			_wap_messagebox_paint(obj);
			break;
		}
		case WA_SIGNAL_NOTIFY_PARENT:
		{	
			break;
		}
		case WA_SIGNAL_USER_KEY_ENTER:
		{	
			wap_messagebox_ms_return = 1;
			wa_screen_switch(WAP_WIN_MESSAGEBOX_MS_ID, WA_SCREEN_SWITCH_TYPE_CENTER_HV_SIZE_ENTER, 500, wa_nok, wa_nok);
			break;
		}
		case WA_SIGNAL_USER_KEY_ESC:
		{	
			wa_screen_switch(WAP_WIN_MENU_ID, WA_SCREEN_SWITCH_TYPE_LEFT_TOGETHER, 500, wa_nok, wa_nok);
			break;
		}
		default:
		{
			break;
		}
	}
}

static void wap_messagebox_ms_create(void)
{
	wap_messagebox_ms = wa_win_create(NULL);
	wa_win_set_pos(wap_messagebox_ms, 14, 12);
	wa_win_set_size(wap_messagebox_ms, 100, 40);
	wa_win_set_edge_show(wap_messagebox_ms, wa_ok);
	wa_win_set_edge_size(wap_messagebox_ms, 1);
	wa_win_set_callback(wap_messagebox_ms, wap_messagebox_ms_callback);
	wap_messagebox_ms_screen.win = wap_messagebox_ms;
	
	wap_messagebox_label_ms = wa_label_create(NULL);
	wa_win_add_obj(wap_messagebox_ms, wap_messagebox_label_ms);
	wa_obj_set_pos(wap_messagebox_label_ms, 0, 0);
	wa_obj_set_size(wap_messagebox_label_ms, 98, 38);
	wa_label_set_font(wap_messagebox_label_ms, &font_song_16_ascii);
	wa_label_set_text(wap_messagebox_label_ms, "i am\nmessagebox");
	wa_label_set_col_space(wap_messagebox_label_ms, 0);
	wa_label_set_row_space(wap_messagebox_label_ms, 0);
	wa_label_set_text_color(wap_messagebox_label_ms, WA_COLOR_FOREGROUND);
	wa_label_set_back_color(wap_messagebox_label_ms, WA_COLOR_BACKGROUND);
	wa_label_set_align_mode(wap_messagebox_label_ms, WA_LABEL_AL_HCENTER | WA_LABEL_AL_VCENTER);
}

static void wap_messagebox_ms_del(void)
{
	wa_obj_del(wap_messagebox_ms);
}

static void _wap_messagebox_ms_paint(wa_obj* obj)
{
}

static void wap_messagebox_ms_callback(wa_sign* sign)
{
	int sign_id;
	wa_obj* obj;
	sign_id = sign->sign_id;
	obj = sign->obj_dst;

	switch (sign_id)
	{
		case WA_SIGNAL_PAINT:
		{
			_wap_messagebox_ms_paint(obj);
			break;
		}
		case WA_SIGNAL_NOTIFY_PARENT:
		{	
			break;
		}
		case WA_SIGNAL_USER_KEY_UP:
		{	
			wa_screen_switch(WAP_WIN_MESSAGEBOX_ID, WA_SCREEN_SWITCH_TYPE_TOP_MOVE_QUIT, 500, wa_nok, wa_nok);
			break;
		}
		case WA_SIGNAL_USER_KEY_DOWN:
		{	
			wa_screen_switch(WAP_WIN_MESSAGEBOX_ID, WA_SCREEN_SWITCH_TYPE_BOTTOM_MOVE_QUIT, 500, wa_nok, wa_nok);
			break;
		}
		case WA_SIGNAL_USER_KEY_LEFT:
		{	
			wa_screen_switch(WAP_WIN_MESSAGEBOX_ID, WA_SCREEN_SWITCH_TYPE_LEFT_MOVE_QUIT, 500, wa_nok, wa_nok);
			break;
		}
		case WA_SIGNAL_USER_KEY_RIGHT:
		{	
			wa_screen_switch(WAP_WIN_MESSAGEBOX_ID, WA_SCREEN_SWITCH_TYPE_RIGHT_MOVE_QUIT, 500, wa_nok, wa_nok);
			break;
		}
		case WA_SIGNAL_USER_KEY_ENTER:
		{	
			wa_screen_switch(WAP_WIN_MESSAGEBOX_ID, WA_SCREEN_SWITCH_TYPE_BOXS_HOR, 500, wa_nok, wa_nok);
			break;
		}
		case WA_SIGNAL_USER_KEY_ESC:
		{	
			wa_screen_switch(WAP_WIN_MESSAGEBOX_ID, WA_SCREEN_SWITCH_TYPE_CENTER_HV_SIZE_QUIT, 500, wa_nok, wa_nok);
			break;
		}
		default:
		{
			break;
		}
	}
}

